import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager

#1、打开浏览器---驱动管理
ChromeIns = ChromeDriverManager().install()
#创建谷歌浏览器驱动对象
driver = webdriver.Chrome(service=Service(ChromeIns))
driver.get("https://www.baidu.com")

#点击百度首页的图片功能,跳转到百度图片页面
print("before:"+driver.title)           #百度首页的标题
print("before:"+driver.current_url)    #百度首页的URL
driver.find_element(By.CSS_SELECTOR,"#s-top-left > a:nth-child(6)").click()  #点击百度图片链接

#获取当前页面的句柄---第一个标签页
curHandle = driver.current_window_handle
#获取所有句柄
allHandle = driver.window_handles
#遍历所有的句柄,切换到新的页面
for handle in allHandle:
    if handle != curHandle:
        #切换句柄
        driver.switch_to.window(handle)

#测试跳转结果
print("after:"+driver.title)           #百度图片的标题
print("after:"+driver.current_url)     #百度图片的url


print("========================================================================")
print("befor:"+driver.title)            #百度图片的标题
print("befor:"+driver.current_url)      #百度图片的URL
driver.find_element(By.CSS_SELECTOR,"#wrapper_main_box > div.wrapper_userinfo_box > div.bidu_top_user_info > div.sites > a:nth-child(2)").click()
time.sleep(3)
print("after:"+driver.title)            #hao123的标题
print("after:"+driver.current_url)      #hao123的url

driver.quit()